/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    ymodem.h
  * @brief   This file contains all the function prototypes for the ymodem.c file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef __YMODEM_H
#define __YMODEM_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32f1xx_hal.h"
#include "flash_on_chip.h"
/* USER CODE END Includes */
/* 数据包大小定义 */
#define YMODEM_PACKET_SIZE_128  128
#define YMODEM_PACKET_SIZE_1024 1024
/* Exported types ------------------------------------------------------------*/
/* YMODEM协议状态枚举 */
typedef enum {
    YMODEM_STATE_IDLE,
    YMODEM_STATE_SEND_C,
    YMODEM_STATE_WAIT_SOH,
    YMODEM_STATE_RECEIVE_PACKET,
    YMODEM_STATE_FIRST_PACKET,  
    YMODEM_STATE_VERIFY_PACKET,
    YMODEM_STATE_SEND_ACK,
    YMODEM_STATE_WAIT_EOT,
    YMODEM_STATE_SEND_NAK_EOT,
    YMODEM_STATE_FINISH,
    YMODEM_STATE_SKIP,
    YMODEM_STATE_ERROR
} ymodem_state_t;
typedef struct _CircularBuffer
{
    uint8_t head;
    uint8_t tail;
    // uint16_t size;
    uint8_t data[YMODEM_PACKET_SIZE_1024+5];
} CircularBuffer_t;

/* CRC相关定义 */
#define CRC16_POLYNOMIAL 0x1021  /* CRC-16-CCITT多项式 */

/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
/* 初始化YMODEM接收 */
void YMODEM_Init(void);
/* YMODEM状态机处理函数 */
void YMODEM_Process(void);
/* 处理接收到的字符 */
void YMODEM_ProcessReceivedData(uint8_t *data, uint16_t size);
/* 计算CRC16校验 */
uint16_t YMODEM_CalculateCRC16(uint8_t *data, uint16_t size);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

#ifdef __cplusplus
}
#endif

#endif /* __YMODEM_H */
